<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/5/9
 * Time: 15:47
 */

namespace app\zyhsapi\controller\v5;

use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\comment\ZyhsArticleComment;
use app\zyhsapi\model\comment\ZyhsArticleCommentReply;
use app\zyhsapi\model\ZyhsAstrBindUser;
use app\zyhsapi\model\ZyhsBlacklist;
use app\zyhsapi\model\ZyhsPxyzUserQuestion;
use think\response\Json;

class Blacklist extends ZyhsAuth
{
    /**
     * 列表
     * @return mixed
     * @throws \Exception
     */
    public function index()
    {
        $data = (new ZyhsBlacklist())->getBlackList(1, $this->user['id'], ['b_uid', 'id']);
        $userBindAstrInfo = (new ZyhsAstrBindUser())->getAstrBindUserData();
        $zyhsArticleCommentModel = new ZyhsArticleComment();
        $res = [];
        foreach ($data['data'] as $datum)
        {
            $userAvatarNicknameData = $zyhsArticleCommentModel->getUserAvatarNickname($datum['b_uid'], $userBindAstrInfo);
            $userAvatarNicknameData['id'] = $datum['id'];
            $userAvatarNicknameData['astr_id'] = $userBindAstrInfo[$datum['b_uid']] ?? 0;
            $res[] = $userAvatarNicknameData;
        }
        $data['data'] = $res;
        return show(config('code.success'), 'success.', $data);
    }

    /**
     * 添加
     * @return Json
     * @throws \Exception
     */
    public function save()
    {
        $type = input('type/d', 0);//1=>平行宇宙问题  2=>平行宇宙评论  3=>平行宇宙回复
        $id = input('id/d', 0);
        switch ($type)
        {
            case 1:
                $data = (new ZyhsPxyzUserQuestion())->getInfoById($id, ['id', 'uid']);
                break;
            case 2:
                $data = (new ZyhsArticleComment())->getInfoById($id, ['id', 'uid']);
                break;
            case 3:
                $data = (new ZyhsArticleCommentReply())->getInfoById($id, ['id', 'uid']);
                break;
            default:
                return show(config('code.error'), '请求异常');
        }
        if (!$data)
        {
            return show(config('code.error'), '请求异常');
        }
        $backList = (new ZyhsBlacklist())->getBlackList(1, $this->user['id'], ['b_uid'], -1);
        if ($backList['data'])
        {
            $bUids = array_column($backList['data'], 'b_uid');
            if (in_array($data['uid'], $bUids, false))
            {
                return show(config('code.success'), 'success.');
            }
        }
        (new ZyhsBlacklist())->saveData(1, $this->user['id'], $data['uid']);
        return show(config('code.success'), 'success.');
    }

    /**
     * 更新
     * @return Json
     * @throws \Exception
     */
    public function update()
    {
        $id = input('id/d', 0);
        $info = (new ZyhsBlacklist())->getInfoById($id);
        if (!$info || $info['uid'] !== $this->user['id'] || $info['delete_time'])
        {
            return show(config('code.success'), 'success.');
        }
        (new ZyhsBlacklist())->updateData(1, $this->user['id'], $id, ['delete_time' => $this->request->time()]);
        return show(config('code.success'), 'success.');
    }
}
